<template>
  <div>
    <div class="data">
      <div class="searchForm">
        <el-form :inline="true" :model="params" class="demo-form-inline">
          <el-form-item label="标题">
              <el-input
                v-model="params.title"
                placeholder="请输入内容"
                prefix-icon="el-icon-search"
                class="block">
              </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="info"
              icon="el-icon-search"
              @click="getList(1)"
              class="block"
              >查询</el-button>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="add"
              >增加</el-button>
              <el-button
              type="danger"
              icon="el-icon-delete"
              @click="removeMore"
              >批量删除</el-button>
          </el-form-item>
         </el-form>   
      </div>
    </div>
    <div class="message">
      <div class="message1">
        <el-table :data="rows" stripe style="width:100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="img" label="图片">
            <template slot-scope="scope">
              <!-- {{scope.row.img}} -->
               <img style="max-width:40px;max-height:40px;" v-if="scope.row.img" :src="scope.row.img" />
            </template>
          </el-table-column>
          <el-table-column prop="title" label="标题">
          </el-table-column>
          <el-table-column prop="url" label="跳转地址">
          </el-table-column>
          <el-table-column prop="status" label="是否启用">
            <template slot-scope="scope">
              <!-- {{scope.row.status}} -->
              <!-- <span v-if="scope.row.status == 1">是</span>
              <span v-else>否</span> -->
              <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="statusChange(scope.row)"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                style="margin-right:10px"
                @click="edit(scope.row)"
                >编辑</el-button>
                <el-popconfirm
                  title="确定删除此数据？"
                  @confirm="remove(scope.row)"
                >
                  <el-button 
                  slot="reference"
                  type="danger"
                  size="mini"
                  icon="el-icon-delete">删除</el-button>
                </el-popconfirm>
            </template>
         </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="pagination">
      <el-pagination
        @size-change="sizeChange"
        @current-change="getList"
        :current-page="params.pageNum"
        :page-sizes="[5, 10, 100, 200, 300, 400]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <CategoryEdit @load="getList" ref="categoryEditer"></CategoryEdit>
  </div>
</template>

<script>
import CategoryEdit from './categoryEdit'
export default {
  name: "shouyeCategory",
  components: {
    CategoryEdit
  },
  data() {
    return{
      ///查询参数
      params:{
        title:'',
        type:'category',
        pageNum:1,
        pageSize:5
      },
      rows:[],
      total:0,
      selecteds:[]
    }
  },
  methods: {
    add(){
      this.$refs.categoryEditer.add()
    },
    edit(row){
      this.$refs.categoryEditer.edit(row)
    },
    ///获取列表
    getList(pageNum){
      if(pageNum) this.params.pageNum = pageNum
      this.$http({
        url:'/admin/index',
        params:this.params
      }).then(res => {
        this.total = res.total
        this.rows = res.rows
      })
    },
    ///是否启用
    statusChange(row){
      console.log(row)
      this.$http({
        url:'/admin/index',
        method:'PUT',
        data:{
          indexId:row._id,
          status:row.status
        }
      }).then(res => {
        ///修改失败
        if(res.errorMsg){
          this.getList()
        }
      })
    },
    ///修改pageSize
    sizeChange(pageSize){
      this.params.pageSize = pageSize;
      this.getList(1)
    },
    handleSelectionChange(rows){
      ///存储选中数据
      this.selecteds = rows
    },
    ///批量删除
    removeMore(){
      /// 获取被选中的数据
      let checkedRowsPrmose = []
      let success = [], fail = []

      for(let i = 0;i < this.selecteds.length; i++){
        checkedRowsPrmose.push(
          new Promise(resovle => {
            let row = this.selecteds[i]
            this.$http({
              url:'/admin/index',
              method:'DELETE',
              params:{indexId:row._id}
            }).then(res => {
              if(!res.errorMsg){
                success.push(row._id)
              }else{
                fail.push(row._id)
              }
              resovle()
            })
          })
        )
      }
      this.$confirm('此操作将删除选中的数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ///删除
        Promise.all(checkedRowsPrmose).then(() => {
          this.$message({
            message: `批量删除完成,${success.length}条数据成功删除,${fail.length}条数据删除失败`,
            duration:3000
          });
          this.getList(1)
        })
      })
    },
    ///删除数据
    remove(row){
      console.log(row)
      this.$http({
        url:'/admin/index',
        method:'DELETE',
        params:{indexId:row._id}
      }).then(res => {
        if(!res.errorMsg){
          this.getList(1)
        }
      })
    },
  },
  created() {
   this.getList()
  },
  mounted() {}
};
</script>

<style scoped>
  .data{
    width: 98%;
    height: 50px;
    background-color: #ffffff;
    margin-top: 30px;
    margin-left: 1%;
  }
  .searchForm{
    position: absolute;
    margin-top: 5px;
    margin-left: 10px;
  }
  .message{
    margin-top: 10px;
    width: 98%;
    margin-left: 1%;
  }
  .pagination{
    margin-top: 5px;
    margin-left: 10px;
  }
</style>